
<template>
  <el-dialog v-model="dialogFormVisible" :title="title" width="1200">
    <el-form :model="form" ref="formRef" label-width="120" style="max-width: 1200px" :rules="rules">
      <div class="form-scroll">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="副本名称" prop="copyName">
              <el-input v-model="form.copyName" placeholder="请填写副本名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="副本描述" prop="copyDesc">
              <el-input v-model="form.copyDesc" placeholder="请填写副本描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="副本排序" prop="copySort">
              <el-input v-model="form.copySort" placeholder="请填写副本排序" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="副本掉落" prop="copyDrops">
              <el-table :data="form.copyDrops" border style="width: 100%">
                <el-table-column prop="dropsUrl" label="图片" >
                  <template v-slot="scope">
                    <imageUpload v-model="scope.row.dropsUrl"></imageUpload>
                  </template>
                </el-table-column>
                <el-table-column prop="dropsName" label="名称" >
                  <template v-slot="scope">
                    <el-input v-model="scope.row.dropsName" placeholder="请输入掉落名称"></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="dropsName" label="操作" >
                  <template v-slot="scope">
                    <el-button type="danger" @click="delDrops(scope.$index)" style="margin-top: 10px">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <el-button type="primary" @click="addDrops" style="margin-top: 10px">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div>
      <el-button type="primary" @click="onSubmit">{{ isEdit? '编辑': '新增' }}</el-button>
      <el-button @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
    // 方法
    import { ref, defineExpose, defineEmits } from 'vue'
    const instance = getCurrentInstance();
    const { proxy } = instance || {};
    const { face_type } = proxy.useDict("face_type")
    import methods from '@/utils/methods.js'

    // 接口
    import { addCopy, getCopy, updateCopy } from '@/api/jx3/copy.js'

    // 组件
    import imageUpload from '@/components/ImageUpload/index.vue'

    // 显示表单
    const dialogFormVisible = ref(false)
    // 弹窗标题
    const title = ref('新增副本')
    // 是否为编辑
    const isEdit = ref(false)
    // 表单节点
    const formRef = ref(null)
    // 表单
    const form = ref({
      id: undefined, // 唯一标识
      copyName: undefined, // 副本名称
      copyDesc: undefined, // 副本描述
      copySort: undefined, // 副本排序
      createTime: undefined, // 创建时间
      createBy: undefined, // 创建人
      updateTime: undefined, // 更新时间
      copyDrops: [], // 掉落列表
    })
    // 表单重置
    const formYs = ref({
      id: undefined, // 唯一标识
      copyName: undefined, // 副本名称
      copyDesc: undefined, // 副本描述
      copySort: undefined, // 副本排序
      createTime: undefined, // 创建时间
      createBy: undefined, // 创建人
      updateTime: undefined, // 更新时间
      copyDrops: [], // 掉落列表
    })
    // 验证
    const rules = ref({
      // 副本名称
      copyName: [
        { required: true, message: '请填写副本名称', trigger: 'blur' }
      ],
      // 副本描述
      copyDesc: [
        { required: true, message: '请填写副本描述', trigger: 'blur' }
      ],
      // 副本排序
      copySort: [
        { required: true, message: '请填写副本排序', trigger: 'blur' }
      ],
      // 创建时间
      createTime: [
        { required: true, message: '请填写创建时间', trigger: 'blur' }
      ],
      // 创建人
      createBy: [
        { required: true, message: '请填写创建人', trigger: 'blur' }
      ],
      // 更新时间
      updateTime: [
        { required: true, message: '请填写更新时间', trigger: 'blur' }
      ],
    })

    const emits = defineEmits(['change'])

    // 添加掉落
    const addDrops = () => {
      form.value.copyDrops.push({
        dropsUrl: '',
        dropsName: '',
      })
    }
    // 删除掉落
    const delDrops = (index) => {
      form.value.copyDrops.splice(index, 1)
    }

    // 打开方法
    const open = () => {
      for (let i in form.value) {
        form.value[i] = formYs.value[i]
      }
      dialogFormVisible.value = true
      isEdit.value = false
      title.value = '新增副本'
    }
    // 编辑
    const edit = (data) => {
      getCopy(data.id).then(res => {
        for (let i in res.data) {
          form.value[i] = data[i]
        }
        form.value.copyDrops = form.value.copyDrops? JSON.parse(form.value.copyDrops) :[]
      })
      isEdit.value = true
      dialogFormVisible.value = true
      title.value = '编辑副本'
    }
    // 关闭方法
    const close = () => {
      dialogFormVisible.value = false
    }

    // 提交
    const onSubmit = async () => {
      await formRef.value.validate((valid, fields) => {
        if (valid) {
          const params = methods.deepClone(form.value)
          params.copyDrops = JSON.stringify(params.copyDrops)
          if(params.id) {
            updateCopy(params).then(res => {
              if(res.code === 200) {
                proxy.$modal.msgSuccess(title.value + '成功');
                close()
                emits('change')
              }
            })
          }else {
            addCopy(params).then(res => {
              if(res.code === 200) {
                proxy.$modal.msgSuccess(title.value + '成功');
                close()
                emits('change')
              }
            })
          }
        }
      })
    }

    defineExpose({
      open, close, edit
    })
</script>

<style scoped lang="scss">
  .table-frame {
    margin: 10px 0;
  }
</style>
